package com.example.asus.homework11.activity;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


import com.example.asus.homework11.BaseListner;
import com.example.asus.homework11.BaseModel;
import com.example.asus.homework11.R;
import com.example.asus.homework11.adapter.UserCartAdapter;
import com.example.asus.homework11.bean.CartBean;

import java.util.List;

public class UserCartActivity extends AppCompatActivity {
    private ImageView topbar_back;
    private Button btn_confirm;
    private RecyclerView recyclerView;
    private UserCartAdapter adapter;
    private int userid;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_cart);
        initView();
        initData();
        initEvent();
    }

    private void initView(){
        topbar_back=findViewById(R.id.topbar_back);
        btn_confirm=findViewById(R.id.btn_confirm);
        recyclerView=findViewById(R.id.rv);
        adapter=new UserCartAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

    private void initData(){
        sharedPreferences = getSharedPreferences("User",MODE_PRIVATE);
        userid = Integer.parseInt(sharedPreferences.getString("userid","获取id失败"));
        BaseModel<List<CartBean>> model = new BaseModel<>();
        model.callEnqueue(model.service.getCart(userid),listner);
    }

    private void initEvent(){
        topbar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseModel<List<CartBean>> model=new BaseModel<>();
                model.callEnqueue(model.service.getItemsFromCart(adapter.getCartList()),buyListner);
            }
        });
    }

    BaseListner<List<CartBean>> listner = new BaseListner<List<CartBean>>() {
        @Override
        public void onResponse(List<CartBean> beans) {
            if (beans==null)
                Toast.makeText(UserCartActivity.this,"Kong",Toast.LENGTH_SHORT).show();
            else{
                adapter.setData(beans);

            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(UserCartActivity.this,msg,Toast.LENGTH_SHORT).show();
        }
    };

    BaseListner<List<CartBean>> buyListner = new BaseListner<List<CartBean>>() {
        @Override
        public void onResponse(List<CartBean> beans) {
            if (beans==null)
                Toast.makeText(UserCartActivity.this,"Kong",Toast.LENGTH_SHORT).show();
            else{
                Toast.makeText(UserCartActivity.this,"下单成功",Toast.LENGTH_SHORT).show();
                finish();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(UserCartActivity.this,msg,Toast.LENGTH_SHORT).show();
        }
    };
}
